<?php

namespace Endor\EndorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Endor\EndorBundle\Entity\Entradas;
use Endor\EndorBundle\Form\EntradasType;
use \Endor\EndorBundle\Model\InventarioModel;

/**
 * Entradas controller.
 *
 */
class EntradasController extends Controller {

    /**
     * Lists all Entradas entities.
     *
     */
    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();

        $entities = $em->getRepository('EndorBundle:Entradas')->findAll();

        return $this->render('EndorBundle:Entradas:index.html.twig', array(
                    'entities' => $entities
                ));
    }

    /**
     * Finds and displays a Entradas entity.
     *
     */
    public function showAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('EndorBundle:Entradas')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Entradas entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('EndorBundle:Entradas:show.html.twig', array(
                    'entity' => $entity,
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Displays a form to create a new Entradas entity.
     *
     */
    public function newAction() {
        $entity = new Entradas();
        $form = $this->createForm(new EntradasType(), $entity);

        return $this->render('EndorBundle:Entradas:new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView()
                ));
    }

    /**
     * Creates a new Entradas entity.
     *
     */
    public function createAction() {
        $entity = new Entradas();
        $request = $this->getRequest();
        $form = $this->createForm(new EntradasType(), $entity);
        $form->bindRequest($request);

        $model = new InventarioModel($this->getDoctrine()->getConnection());
        $isValid = TRUE;
        //$isValid = $model->GetDisponible($entity->getCantidad(), $entity->getBodega()->getId(), $entity->getArticulo()->getId());
        
        if ($form->isValid() && $isValid) {
            $fechaVencimiento = $request->get("fechaVencimiento");
            $fechaEntrada = $request->get("fechaEntrada");
            if($fechaVencimiento != null){
                $fechaVencimiento = date_create($fechaVencimiento);
            }else{
                $fechaVencimiento = null;
            }
            $entity->setFechaVencimiento($fechaVencimiento);
            $entity->setFechaEntrada(date_create($fechaEntrada));
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('entradas'));
        }
        
        return $this->render('EndorBundle:Entradas:new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView()
                ));
    }

    /**
     * Displays a form to edit an existing Entradas entity.
     *
     */
    public function editAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('EndorBundle:Entradas')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Entradas entity.');
        }

        $editForm = $this->createForm(new EntradasType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('EndorBundle:Entradas:edit.html.twig', array(
                    'entity' => $entity,
                    'edit_form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Edits an existing Entradas entity.
     *
     */
    public function updateAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('EndorBundle:Entradas')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Entradas entity.');
        }

        $editForm = $this->createForm(new EntradasType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();
        $fechaVencimiento = $request->get("fechaVencimiento");
        $fechaEntrada = $request->get("fechaEntrada");
        
        if(!empty ($fechaVencimiento)){
            $fechaVencimiento = date_create($fechaVencimiento);
        }else{
            $fechaVencimiento = null;
        }
        $entity->setFechaVencimiento($fechaVencimiento);
        $entity->setFechaEntrada(date_create($fechaEntrada));
        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('entradas'));
        }

        return $this->render('EndorBundle:Entradas:edit.html.twig', array(
                    'entity' => $entity,
                    'edit_form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Deletes a Entradas entity.
     *
     */
    public function deleteAction($id) {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('EndorBundle:Entradas')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Entradas entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('entradas'));
    }

    private function createDeleteForm($id) {
        return $this->createFormBuilder(array('id' => $id))
                        ->add('id', 'hidden')
                        ->getForm()
        ;
    }

}
